import 'dart:async';
import 'dart:convert';

import 'package:cron/cron.dart';
import 'package:flutter/material.dart';
import 'package:super_tool_server/business/ws_manager.dart';
import 'package:super_tool_server/component/button/round_button.dart';
import 'package:super_tool_server/model/message_info.dart';
import 'package:super_tool_server/page/home/log_widget.dart';

class ServerWidget extends StatefulWidget {
  const ServerWidget({super.key});

  @override
  State<ServerWidget> createState() => _ServerWidgetState();
}

class _ServerWidgetState extends State<ServerWidget> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 20),
      child: Column(
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 45,
                child: Visibility(
                  visible: true,
                  child: RoundButton(
                    text: '启动服务',
                    style: RoundButtonStyle.normal,
                    shape: RoundButtonShape.round,
                    size: RoundButtonSize.normal,
                    fontSize: 14,
                    textColor: Colors.red,
                    borderRadius: 5,
                    onPress: () async {
                      await WebsocketManager.getInstance().startServer();
                    },
                  ),
                ),
              ),
              SizedBox(
                width: 20,
              ),
              Visibility(
                visible: false,
                child: Row(
                  children: [
                    SizedBox(
                      height: 45,
                      child: RoundButton(
                        text: '[右移] 指令',
                        style: RoundButtonStyle.normal,
                        shape: RoundButtonShape.round,
                        size: RoundButtonSize.normal,
                        fontSize: 14,
                        textColor: Colors.red,
                        borderRadius: 5,
                        onPress: () async {
                          void move() {
                            Map<String, dynamic> jsonData = {};
                            jsonData['distance'] = 10;
                            WebsocketManager.getInstance().sendMsgToUE(MessageInfo()
                              ..cmdType = 'MOVE_RIGHT'
                              ..param = jsonEncode(jsonData));
                          }

                          move();
                          // Timer.periodic(const Duration(seconds: 5), (timer) {
                          //
                          // });
                        },
                      ),
                    ),
                    SizedBox(
                      width: 20,
                    ),
                    SizedBox(
                      height: 45,
                      child: RoundButton(
                        text: '[左移] 指令',
                        style: RoundButtonStyle.normal,
                        shape: RoundButtonShape.round,
                        size: RoundButtonSize.normal,
                        fontSize: 14,
                        textColor: Colors.red,
                        borderRadius: 5,
                        onPress: () async {
                          Map<String, dynamic> jsonData = {};
                          jsonData['distance'] = 10;
                          WebsocketManager.getInstance().sendMsgToUE(MessageInfo()
                            ..cmdType = 'MOVE_LEFT'
                            ..param = jsonEncode(jsonData));
                        },
                      ),
                    ),
                    SizedBox(
                      width: 20,
                    ),
                    SizedBox(
                      height: 45,
                      child: RoundButton(
                        text: '[前进] 指令',
                        style: RoundButtonStyle.normal,
                        shape: RoundButtonShape.round,
                        size: RoundButtonSize.normal,
                        fontSize: 14,
                        textColor: Colors.red,
                        borderRadius: 5,
                        onPress: () async {
                          Map<String, dynamic> jsonData = {};
                          jsonData['distance'] = 10;
                          WebsocketManager.getInstance().sendMsgToUE(MessageInfo()
                            ..cmdType = 'MOVE_FORWARD'
                            ..param = jsonEncode(jsonData));
                        },
                      ),
                    ),
                    SizedBox(
                      width: 20,
                    ),
                    SizedBox(
                      height: 45,
                      child: RoundButton(
                        text: '[后退] 指令',
                        style: RoundButtonStyle.normal,
                        shape: RoundButtonShape.round,
                        size: RoundButtonSize.normal,
                        fontSize: 14,
                        textColor: Colors.red,
                        borderRadius: 5,
                        onPress: () async {
                          Map<String, dynamic> jsonData = {};
                          jsonData['distance'] = 10;
                          WebsocketManager.getInstance().sendMsgToUE(MessageInfo()
                            ..cmdType = 'MOVE_BACKWARD'
                            ..param = jsonEncode(jsonData));
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          const SizedBox(
            width: 20,
            height: 20,
          ),
          Expanded(
            child: LogPanelWidget(
              autoScrollLog: true,
            ),
          ),
        ],
      ),
    );
  }
}
